package com.we823.cms.repository.service.beetl.tag;

import java.util.List;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.we823.cms.model.cms.News;
import com.we823.cms.repository.service.cms.NewsService;

import javax.annotation.Resource;

@Service("cms_news_list")
@Scope("prototype")
public class NewsListTag extends AbstractListTag {

	private static final String PARAM_NEWS_CHANNEL="channel";

	private @Resource NewsService newsService;
	
	@Override
	public void execute() {
		int channelId = getParam(PARAM_NEWS_CHANNEL, 0);
		PageInfo<News> newsPage;
		List<News> newsList = null;
		
		boolean hasMore = false;
		if(pageRequest==null){
			if(channelId==0){
				newsList = newsService.findAll(sort);
			}else{
				newsList = newsService.findByChannel(channelId,sort);
			}
		}else{
			if(channelId==0){
				newsPage = newsService.findAll(pageRequest);
			}else{
				newsPage = newsService.findByNewsChannel(channelId,pageRequest);
			}
			if(newsPage!=null){
				newsList = newsPage.getList();
				hasMore = (newsPage.getTotal() > count);
			}
		}

		this.binds(hasMore,newsList);
	}
}
